package com.situ.company.score.service;

import java.util.List;

import com.situ.company.score.dao.ScoreDao;
import com.situ.company.score.model.ScoreModel;

public class ScoreService {
	
	ScoreDao dao = new ScoreDao();
	
	public String insert(ScoreModel model) {
		ScoreModel mod = selectModel(model);
		if (mod != null) {
			return "E";
		}
		Integer result = dao.insert(model);
		System.out.println(result);
		return String.valueOf(result);
	}
	
	public String delete(ScoreModel model) {
		Integer result = dao.delete(model);
		return String.valueOf(result);
	}
	
	public String update(ScoreModel model,List<Object> objs) {
		Integer result = dao.update(model,objs);
		return String.valueOf(result);
	}
	
	/**
	 * 多条件+模糊查询 --一些记录
	 * 
	 * @param model
	 * @return
	 */
	public List<ScoreModel> selectList(ScoreModel model) {
		String empcode = model.getEmpcode();
		System.out.println(empcode);
		if (empcode != null) {
			model.setEmpcode("%" + empcode + "%");
		}
		String procode = model.getProcode();
		System.out.println(procode);
		if (procode != null) {
			model.setProcode("%" + procode + "%");
		}
		return dao.selectList(model);
	}
	
	/**
	 * 根据联合主键准确查询出唯一记录，主键不存在返回null
	 * 
	 * @param model
	 * @return
	 */
	public ScoreModel selectModel(ScoreModel model) {
		ScoreModel temp = new ScoreModel();
		temp.setEmpcode(model.getEmpcode());
		temp.setProcode(model.getProcode());
		List<ScoreModel> list = dao.selectList(temp);
		if (list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}
	
	/**
	 * 根据多条件的模糊查询出的记录条数
	 * 
	 * @param model
	 * @return
	 */
	public int selectCount(ScoreModel model) {
		ScoreModel temp = new ScoreModel();
		String empcode = model.getEmpcode();
		temp.setEmpcode(empcode == null ? "%%" : "%" + empcode + "%");
		String procode = model.getProcode();
		temp.setProcode(procode == null ? "%%" : "%" + procode + "%");
		temp.setPageOn(false);
		return dao.selectCount(temp);
	}
	
}
